{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Python中一些推荐和建议"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 课程思政\n",
    "撸起袖子加油干\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 3\n",
      "4 3\n"
     ]
    }
   ],
   "source": [
    "#数据交换\n",
    "a = 3\n",
    "b = 4\n",
    "temp = a\n",
    "a = b\n",
    "b = temp\n",
    "print(a,b)\n",
    "#推荐做法\n",
    "x = 3\n",
    "y = 4\n",
    "x, y = y, x\n",
    "print(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号:1;姓名:哈哈\n",
      "学号:2;姓名:呵呵\n",
      "学号:3;姓名:嘿嘿\n",
      "学号:1;姓名:哈哈\n",
      "学号:2;姓名:呵呵\n",
      "学号:3;姓名:嘿嘿\n"
     ]
    }
   ],
   "source": [
    "#循环获取元素\n",
    "#循环获取zip元素\n",
    "stunos = [1,2,3]\n",
    "names = ['哈哈','呵呵','嘿嘿']\n",
    "for stu in zip(stunos, names):\n",
    "    stuno, name = stu\n",
    "    print(\"学号:{};姓名:{}\".format(stuno, name))\n",
    "#推荐做法\n",
    "for stuno, name in zip(stunos, names):\n",
    "    print(\"学号:{};姓名:{}\".format(stuno, name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号:1\n",
      "姓名:哈哈\n"
     ]
    }
   ],
   "source": [
    "#循环获取字典元素\n",
    "stus = {\"学号\":1,\"姓名\":\"哈哈\"}\n",
    "for key in stus:\n",
    "    val = stus[key]\n",
    "    print(\"{}:{}\".format(key,val))\n",
    "#推荐做法\n",
    "for key, val in stus.items():\n",
    "    print(\"{}:{}\".format(key, val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "哈哈\n",
      "呵呵\n",
      "嘿嘿\n",
      "哈哈\n",
      "呵呵\n",
      "嘿嘿\n"
     ]
    }
   ],
   "source": [
    "#迭代访问\n",
    "for index in range(len(names)):\n",
    "    item = names[index]\n",
    "    print(item)\n",
    "#推荐\n",
    "for item in names:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 哈哈\n",
      "1 呵呵\n",
      "2 嘿嘿\n",
      "0 哈哈\n",
      "1 呵呵\n",
      "2 嘿嘿\n"
     ]
    }
   ],
   "source": [
    "#迭代访问\n",
    "for index in range(len(names)):\n",
    "    item = names[index]\n",
    "    print(index, item)\n",
    "#推荐做法\n",
    "for index, item in enumerate(names):\n",
    "    print(index, item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "哈哈呵呵嘿嘿\n",
      "哈哈呵呵嘿嘿\n"
     ]
    }
   ],
   "source": [
    "#字符串连接\n",
    "result = ''\n",
    "for name in names:\n",
    "    result += name\n",
    "print(result)\n",
    "#推荐做法\n",
    "result = ''.join(names)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "哈哈,呵呵,嘿嘿\n",
      "哈哈,呵呵,嘿嘿\n"
     ]
    }
   ],
   "source": [
    "#字符串连接\n",
    "result = ''\n",
    "for name in names:\n",
    "    result += name + ','\n",
    "result = result[:-1]\n",
    "print(result)\n",
    "#推荐做法\n",
    "result = ','.join(names)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TOM,JOHN,JERRY\n",
      "0:00:00\n",
      "TOM,JOHN,JERRY\n",
      "0:00:00\n"
     ]
    }
   ],
   "source": [
    "#如何减少内存使用量，在名字数量很多的情况下就很明显了\n",
    "names = ['tom','john','jerry']\n",
    "#先全部计算出所有大写放内存，然后再join\n",
    "start = datetime.now()\n",
    "print(','.join([name.upper() for name in names]))\n",
    "end = datetime.now()\n",
    "print(end-start)\n",
    "#推荐做法\n",
    "names = ['tom','john','jerry']\n",
    "print(','.join(name.upper() for name in names))\n",
    "end2 = datetime.now()\n",
    "print(end2-end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:00:00.004987\n",
      "0:00:00.000998\n"
     ]
    }
   ],
   "source": [
    "#如何减少内存使用量\n",
    "from datetime import datetime\n",
    "start = datetime.now()\n",
    "map(lambda x: int(x) ** 2, [x for x in range(100000)])\n",
    "end = datetime.now()\n",
    "print(end-start)\n",
    "#推荐做法\n",
    "map(lambda x: int(x) ** 2, (x for x in range(100000)))\n",
    "end2 = datetime.now()\n",
    "print(end2-end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "333328333350000\n",
      "333328333350000\n"
     ]
    }
   ],
   "source": [
    "#如何减少内存使用量\n",
    "print(sum([n ** 2 for n in range(100000)]))\n",
    "#推荐做法\n",
    "print(sum(n ** 2 for n in range(100000)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#0<x<10,这种表达式在c和java都不是我们期望的结果\n",
    "x = 8\n",
    "print(0<x and x<10)\n",
    "#推荐做法\n",
    "print(0 < x < 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n",
      "ok\n"
     ]
    }
   ],
   "source": [
    "#学会使用in来简化编程\n",
    "x = 1\n",
    "if x == 1 or x == 2 or x == 3:\n",
    "    print(\"ok\")\n",
    "#推荐做法\n",
    "if x in [1,2,3]:\n",
    "    print(\"ok\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "撸起袖子加油干\n",
      "撸起袖子加油干\n"
     ]
    }
   ],
   "source": [
    "#学会使用in来简化编程\n",
    "x=\"加油\"\n",
    "y=\"撸起袖子加油干\"\n",
    "\n",
    "if y.find(x) != -1:\n",
    "    print(y)\n",
    "#推荐做法\n",
    "if x in y:\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "#常见逻辑表达式写法\n",
    "if x == True:\n",
    "    print(\"ok\")\n",
    "#推荐做法\n",
    "if x:\n",
    "    print(\"ok\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "有数据\n",
      "有数据\n",
      "有数据\n"
     ]
    }
   ],
   "source": [
    "#常见逻辑表达式写法\n",
    "names = ['tom','john','jerry']\n",
    "if len(names) > 0:\n",
    "    print(\"有数据\")\n",
    "if names != []:\n",
    "    print(\"有数据\")\n",
    "#推荐做法\n",
    "if names:\n",
    "    print(\"有数据\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "有东西\n",
      "有东西\n"
     ]
    }
   ],
   "source": [
    "#常见逻辑表达式写法\n",
    "#对于非None的表达\n",
    "x = []\n",
    "if x != None:\n",
    "    print(\"有东西\")\n",
    "#推荐做法\n",
    "if x is not None:\n",
    "    print(\"有东西\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 哈哈哈哈哈哈哈哈啊哈哈哈\n",
      "哈哈哈啊哈哈哈哈哈哈哈哈\n",
      "> 呵呵呵呵呵呵呵阿斯顿发顺丰阿斯弗\n",
      "弗斯阿丰顺发顿斯阿呵呵呵呵呵呵呵\n"
     ]
    }
   ],
   "source": [
    "#如果对待不用的变量\n",
    "for i in range(10):\n",
    "    x = input(\"请输入要反转的文字：\")\n",
    "    print(x[::-1])\n",
    "#推荐做法\n",
    "for _ in range(10):\n",
    "    x = input(\"请输入要反转的文字：\")\n",
    "    print(x[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#List初始化\n",
    "nones = [None, None, None, None]\n",
    "#推荐做法\n",
    "nones = [None] * 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
